// pages/user_center/travelagency/friendcarcomp/friendcarcomp.js
var Tool = require("../../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    complist: [],
    searchkey: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getfriendcomp()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getfriendcomp: function () {
    var that = this
    that.setData({
      complist: [],
    });
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CooPeratePartners.ashx',
      data: {
        ID: useinfo.ID,
        CType: 1,
        Info: 0,
        Conditions: that.data.searchkey,
        PageIndex: 1,
        PageSize: 1000,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {
          that.setData({
            complist: that.data.complist.concat(data.data),
          });
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  //删除合作司机
  deletecomp: function (e) {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '确定与 ' + e.currentTarget.dataset.name + ' 车公司结束合作关系',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'https://www.lanshuicar.com/Server/AgencyServer/AgencyAddCoopPartners.ashx',
            data: {
              ID: useinfo.ID,
              PID: e.currentTarget.dataset.partnerid,
              Type: 3,
              OpType: 1,
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              console.log(res)
              if (res.data.code == 0) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'succes',
                  duration: 1000,
                })
              }
              that.getfriendcomp()
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },

  //搜索点击事件
  search: function () {
    var that = this
    that.getfriendcomp()
  },

  //输入操作
  inputchange: function (e) {
    var that = this
    console.log(e)

    that.setData({
      searchkey: e.detail.value,
    });
  },

  addfriendcomp: function () {
    wx.navigateTo({
      url: '../friendcarcomp/addfriendcomp/addfriendcomp'
    })
  }
})